﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using System.Data.SqlClient;

namespace sistemaAcademicoFinanceiroEscolas.pagamentoAlunosInsert
{
    public partial class Insert : System.Web.UI.Page
    {
        protected MetaTable table;

        protected void Page_Init(object sender, EventArgs e)
        {
            DynamicDataManager1.RegisterControl(DetailsView1);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            table = DetailsDataSource.GetTable();
            Title = table.DisplayName;
        }

        protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            if (e.CommandName == DataControlCommands.CancelCommandName)
            {
                Response.Redirect("List.aspx?informacao=nao&codigoTipoMaterial=");
            }
        }

        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            if (e.Exception == null || e.ExceptionHandled)
            {
                string validarPassagem = "sim";

                System.Data.SqlClient.SqlConnection sqlc = new System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                sqlc.Open();

                string sqlMax = "SELECT MAX(codigoMaterial) AS codigoMaterial FROM material";

                System.Data.SqlClient.SqlCommand cmdPegar = new System.Data.SqlClient.SqlCommand(sqlMax, sqlc);
                System.Data.SqlClient.SqlDataReader drPegar = cmdPegar.ExecuteReader();
                drPegar.Read();

                string codigoMaterialMax = drPegar["codigoMaterial"].ToString();

                cmdPegar.Dispose();
                drPegar.Close();

                string sqlPegarL = "select * from material where codigomaterial = " + codigoMaterialMax;

                System.Data.SqlClient.SqlCommand cmdPegarL = new System.Data.SqlClient.SqlCommand(sqlPegarL, sqlc);
                System.Data.SqlClient.SqlDataReader drPegarL = cmdPegarL.ExecuteReader();
                drPegarL.Read();

                string descricaoMaterialMax = drPegarL["descricaoMaterial"].ToString();
                string valorMaterial = drPegarL["valorMaterial"].ToString();
                string valorMedioMaterial = drPegarL["valorMedioMaterial"].ToString();
                string marcaMaterial = drPegarL["marcaMaterial"].ToString();
                string quantidadeMaterial = drPegarL["quantidadeMaterial"].ToString();
                string quantidadeMinima = drPegarL["quantidadeMinima"].ToString();
                string quantidadeMaxima = drPegarL["quantidadeMaxima"].ToString();

                cmdPegarL.Dispose();
                drPegarL.Close();


                string descricaoMaterial = "";
                string codigoMaterial = "";

                string sql = "select * from material";

                System.Data.SqlClient.SqlCommand cmdListar = new System.Data.SqlClient.SqlCommand(sql, sqlc);
                System.Data.SqlClient.SqlDataReader drListar = cmdListar.ExecuteReader();

                if (drListar.HasRows)
                {
                    while (drListar.Read())
                    {
                        codigoMaterial = drListar["codigoMaterial"].ToString();
                        descricaoMaterial = drListar["descricaoMaterial"].ToString();

                        descricaoMaterialMax = descricaoMaterialMax.ToUpper();
                        descricaoMaterial = descricaoMaterial.ToUpper();

                        if (codigoMaterial != codigoMaterialMax && descricaoMaterial.Equals(descricaoMaterialMax))
                        {
                            SqlConnection sqlDel = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                            sqlDel.Open();

                            string sqlDeletar = "delete from material where codigoMaterial = " + codigoMaterialMax + "";

                            SqlCommand deletarDetalhe = new SqlCommand(sqlDeletar, sqlDel);
                            deletarDetalhe.ExecuteNonQuery();

                            sqlDel.Close();

                            validarPassagem = "nao";

                            Response.Redirect("Insert.aspx?informacao=sim");
                        }

                        if (Convert.ToDouble(quantidadeMaterial) > Convert.ToDouble(quantidadeMaxima))
                        {
                            ////mensagem java script
                            //string literalcc = "<script language=JavaScript>maxima();</script>";
                            //pnlJS.Controls.Add(new LiteralControl(literalcc));

                            Response.Redirect("List.aspx?informacao=nao&codigoTipoMaterial=");
                        }

                        if (Convert.ToDouble(quantidadeMaterial) < Convert.ToDouble(quantidadeMinima))
                        {
                            ////mensagem java script
                            //string literalcc = "<script language=JavaScript>minima();</script>";
                            //pnlJS.Controls.Add(new LiteralControl(literalcc));

                            Response.Redirect("List.aspx?informacao=nao&codigoTipoMaterial=");
                        }
                    }
                }

                cmdListar.Dispose();
                drListar.Close();
                sqlc.Close();


                if (validarPassagem == "sim")
                {
                    string data = DateTime.Now.ToString("dd/MM/yyyy");
                    string hora = DateTime.Now.ToString("HH:mm");

                    string insertLogAcao = "insert into logAcao (codigoUsuario, dataLogAcao, horaLogAcao, tipoLogAcao, tipoTabelaLogAcao, descricaoLogAcao) ";
                    insertLogAcao = insertLogAcao + "values (" + Session["codigoUsuario"].ToString() + ", '" + data + "', '" + hora + "', 'Cadastro', 'Materiais', ";
                    insertLogAcao = insertLogAcao + "'Código: " + codigoMaterial + ", Descrição: " + descricaoMaterialMax + ", Valor: " + valorMaterial + ", Valor Medio: " + valorMedioMaterial + ", Marca: " + marcaMaterial + ", Quantidade: " + quantidadeMaterial + ", Quantidade maxima: " + quantidadeMaxima + ", Quantidade mínima: " + quantidadeMinima + "')";

                    System.Data.SqlClient.SqlConnection sqly = new System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                    sqly.Open();
                    System.Data.SqlClient.SqlCommand inserirviagem = new System.Data.SqlClient.SqlCommand(insertLogAcao, sqly);
                    inserirviagem.ExecuteNonQuery();
                    inserirviagem.Dispose();
                    sqly.Close();


                    Response.Redirect("List.aspx?informacao=nao&codigoTipoMaterial=");
                }
                else
                {
                    //Response.Redirect(Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/maquinas/Insert.aspx");
                }
            }
        }
    }
}
